<?php

class HeroModel extends Model {

	//获取已有英雄
    public static function getheros($uid = 0)
	{
		$uid = intval($uid);
		if(!$uid) return array('ecode' => ERR302);

		$heros = daocall("hero", "getheros", array($uid));
        if (!is_array($heros)) return array('ecode' => ERR505);

        return array('ecode' => 0, 'ret' => $heros);
	}

	//刷新英雄
    public static function freshhero($uid = 0, $useext = 0)
	{
		$uid = intval($uid);
		if(!$uid) return array('ecode' => ERR302);

		$tm = time();

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));

		$heros = array();
		$needfresh = false;
		if($useext)
		{
			//手动刷新
			$luck = 0;
			$needfresh = true;
			if(date("Ymd") == date("Ymd", $uext['hfreshtm']))
			{
				//当天
				if($uext['hnfresh'] < 2)
				{
					//玩家每天有2次机会免费刷新英雄
					$luck = 1;
				}
				else
				{
					if($uext["mb"] >= 20) $luck = 2;//第三次开始刷新英雄需要花费20浮屠币
				}
			}
			else
			{
				//隔天
				$luck = 1;
			}

			//luck：0表示mb不足，1表示免费刷新，2表示mb刷新
			if(!$luck) return array('ecode' => ERR403);
		}
		else
		{
			//自动刷新
			if($tm - $uext['hfreshtm'] >= 3600 * 4)
			{
				//英雄刷新时间4小时
				$needfresh = true;
			}
		}

		if($needfresh)
		{
			$herosn = 0;
			$n = 0;
			$herosnarr = array();
			while(!$herosn)
			{
				$herosn = self::randhero($uext["lv"], $uext["side"]);
				if($herosn)
				{
					$herosnarr[] = $herosn;
					if(count($herosnarr) >= 3) break;
					$herosn = 0;
				}
				$n ++;
				if($n >= 9) break;
			}

			$sets = array();
			if($luck == 2)
			{
				$uext["mb"] -= 20;
				$sets["mb"] = $uext["mb"];
			}
			$uext["hfreshtm"] = $tm;
			$uext["hnfresh"] ++;
			$uext["hfresh"] = $herosnarr ? implode(",", $herosnarr) : "";
			$sets["hfreshtm"] = $uext["hfreshtm"];
			$sets["hnfresh"] = $uext["hnfresh"];
			$sets["hfresh"] = $uext["hfresh"];
			if(!daocall("user", "setuserext", array($uid, $sets))) return array('ecode' => ERR407);
		}

        return array('ecode' => 0, 'ret' => array(
			"hfreshtm" => $uext["hfreshtm"],
			"hnfresh" => $uext["hnfresh"],
			"hfresh" => $uext["hfresh"],
		));
    }

	//招募英雄
	public static function hirehero($uid = 0, $pos = 0)
	{
		$uid = intval($uid);
		$pos = intval($pos);
		if(!$uid) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);

		$uext["hfresh"] = $uext["hfresh"] ? explode(",", $uext["hfresh"]) : array();
		if(!$uext["hfresh"] || !$uext["hfresh"][$pos]) return array('ecode' => ERR503);

		$heros = daocall("hero", "getheros", array($uid));
        if (!is_array($heros)) return array('ecode' => ERR505);
		if(count($heros) >= $uext["hnpos"]) return array('ecode' => ERR506);

		$conf = &load_conf("pub:hero");
		if(!$conf || !$conf["hero"] || !$conf["hero"][$uext["hfresh"][$pos]]) return array('ecode' => ERR503);
		$conf = $conf["hero"][$uext["hfresh"][$pos]];

		$hid = daocall("hero", "newhero", array($uid, array("hsn" => $uext["hfresh"][$pos])));
		if(!$hid) return array('ecode' => ERR504);

		$uext["hfresh"][$pos] = 0;
		$uext["hfresh"] = implode(",", $uext["hfresh"]);
		$uext["gb"] -= $conf["hiregb"];

		$sets["hfresh"] = $uext["hfresh"];
		$sets["gb"] = $uext["gb"];

		if(!daocall("user", "setuserext", array($uid, $sets))) return array('ecode' => ERR408);

		return array('ecode' => SUCCESS, 'ret' => array("hid" => $hid));
	}

	//解雇英雄
	public static function firehero($uid = 0, $hid = 0)
	{
		$uid = intval($uid);
		$hid = intval($hid);
		if(!$uid || !$hid) return array('ecode' => ERR302);

		$heros = daocall("hero", "getheros", array($uid));
		if(!$heros || !$heros[$hid]) return array('ecode' => ERR505);

		if(!daocall("equip", "clearhid", array($uid))) return array('ecode' => ERR507);
		if(!daocall("hero", "delhero", array($uid, $hid))) return array('ecode' => ERR507);

		return array("ecode" => SUCCESS);
	}

	//开辟英雄位
	public static function openheroseat($uid = 0)
	{
		$uid = intval($uid);
		if(!$uid) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);

		//免费英雄列表数量上限为10，花费500金币可开启1英雄位，开启达到20英雄位后需要花费10浮屠币开启1个英雄位。
		$sets = array();
		if($uext["hnpos"] >= 20)
		{
			if($uext["mb"] < 10) return array('ecode' => ERR403);
			$uext["mb"] -= 10;
			$uext["hnpos"] ++;
			$sets["mb"] = $uext["mb"];
			$sets["hnpos"] = $uext["hnpos"];
		}
		elseif($uext["hnpos"] >= 10)
		{
			if($uext["gb"] < 500) return array('ecode' => ERR404);
			$uext["gb"] -= 500;
			$uext["hnpos"] ++;
			$sets["gb"] = $uext["gb"];
			$sets["hnpos"] = $uext["hnpos"];
		}
		else
		{
			$uext["hnpos"] ++;
			$sets["hnpos"] = $uext["hnpos"];
		}

		if(!daocall("user", "setuserext", array($uid, $sets))) return array('ecode' => ERR407);

		 return array('ecode' => SUCCESS, 'ret' => array("hnpos" => $uext["hnpos"]));
	}

	//随机刷新一个英雄
	private static function randhero($ulv = 1, $side = "")
	{
		//1水手/下忍，2舵手/中忍，3大副/上忍，4船长/影
		$confrate = array(
			1 => array(
				"rate" => 30, //几率
				"openlv" => 1, //开启等级
			),
			2 => array(
				"rate" => 15, //几率
				"openlv" => 10, //开启等级
			),
			3 => array(
				"rate" => 5, //几率
				"openlv" => 20, //开启等级
			),
			4 => array(
				"rate" => 1, //几率
				"openlv" => 30, //开启等级
			),
		);

		$allrate = 0;
		foreach($confrate as $grade => $row)
		{
			if($ulv < $row["openlv"]) unset($confrate[$grade]);
			else $allrate += $row["rate"];
		}
		
		if(!$allrate || !$confrate) return false;

		$conf = &load_conf("pub:hero");
		
		if(!$conf || !$conf["hero"]) return false;

		$heroconf = array();
		foreach($conf["hero"] as $sn => $row)
		{
			if(array_key_exists($row["grade"], $confrate) && $row["side"] == $side) $heroconf[$row["grade"]][$sn] = $row;
		}

		$realrate = rand(1, 100);
		$passrate = 0;
		$herosn = 0;

		foreach($confrate as $grade => $row)
		{
			$rate = round($row["rate"] / $allrate  * 100);
			if($realrate > $passrate && $realrate <= $passrate + $rate)
			{
				if(!$heroconf[$grade]) break;
				$herosn = array_rand($heroconf[$grade]);
				if($herosn) break;
			}
			$passrate += $rate;
		}

		return $herosn;
	}

	//计算英雄属性
	public static function calheroprop($hid = 0)
	{
		
	}

	//佩戴装备
	public static function onequip($uid = 0, $eid = 0, $hid = 0)
	{
		$uid = intval($uid);
		$hid = intval($hid);
		$eid = $eid;
		if(!$uid || !$hid || !$eid) return array('ecode' => ERR302);

		if(!daocall("equip", "setequip", array($uid, $eid, array("hid" => $hid)))) return array('ecode' => ERR604);

		return array("ecode" => SUCCESS);
	}

	//脱下装备
	public static function offequip($uid = 0, $eid = 0, $hid = 0)
	{
		$uid = intval($uid);
		$hid = intval($hid);
		$eid = $eid;
		if(!$uid || !$hid || !$eid) return array('ecode' => ERR302);

		if(!daocall("equip", "setequip", array($uid, $eid, array("hid" => 0)))) return array('ecode' => ERR604);

		return array("ecode" => SUCCESS);
	}
	
}
?>